#Lotka-Volterra模型

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint

# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei']  # 使用 SimHei 字体
plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题

# # Lotka-Volterra 方程
# def lotka_volterra(y, t, alpha, beta, delta, gamma):
#     prey, predator = y
#     dydt = [alpha * prey - beta * prey * predator,
#             delta * prey * predator - gamma * predator]
#     return dydt
#
# # 初始种群数量
# prey_initial = 40
# predator_initial = 9
# y0 = [prey_initial, predator_initial]
#
# # 时间点
# t = np.linspace(0, 200, 1000)
#
# # 参数为常数
# alpha = 0.1  # 被捕食者的繁殖率
# beta = 0.02  # 捕食者捕食被捕食者的效率
# delta = 0.01 # 捕食者繁殖率
# gamma = 0.1  # 捕食者的死亡率
#
# # 求解微分方程
# solution = odeint(lotka_volterra, y0, t, args=(alpha, beta, delta, gamma))
#
# # 绘制结果
# plt.figure(figsize=(10, 5))
# plt.plot(t, solution[:, 0], label='被捕食者种群')
# plt.plot(t, solution[:, 1], label='捕食者种群')
# plt.legend()
# plt.xlabel('时间')
# plt.ylabel('种群数量')
# plt.title('Lotka-Volterra 模型')
# plt.show()


#######################################参数为关于t的函数########################################
# Lotka-Volterra 方程
def lotka_volterra(y, t, alpha_func, beta_func, delta_func, gamma_func):
    prey, predator = y
    alpha = alpha_func(t)
    beta = beta_func(t)
    delta = delta_func(t)
    gamma = gamma_func(t)
    dydt = [alpha * prey - beta * prey * predator,
            delta * prey * predator - gamma * predator]
    return dydt

# 定义参数作为时间的函数
def alpha(t):
    return 0.1 + 0.01 * np.sin(0.05 * t)

def beta(t):
    return 0.02 + 0.005 * np.cos(0.03 * t)

def delta(t):
    return 0.01 + 0.002 * np.sin(0.04 * t)

def gamma(t):
    return 0.1 + 0.01 * np.cos(0.06 * t)

# 初始种群数量
prey_initial = 40
predator_initial = 9
y0 = [prey_initial, predator_initial]

# 时间点
t = np.linspace(0, 200, 1000)

# 求解微分方程
solution = odeint(lotka_volterra, y0, t, args=(alpha, beta, delta, gamma))

# 绘制结果
plt.figure(figsize=(10, 5))
plt.plot(t, solution[:, 0], label='被捕食者种群')
plt.plot(t, solution[:, 1], label='捕食者种群')
plt.legend()
plt.xlabel('时间')
plt.ylabel('种群数量')
plt.title('Lotka-Volterra 模型（参数为时间的函数）')
plt.show()